package net.zjitc.dao.impl;

import net.zjitc.dao.CartDao;
import net.zjitc.pojo.CartItem;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/11.
 */
public class CartDaoImpl implements CartDao {
    QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());

    public List<CartItem> findByUid(int uid) {
        String sql = "SELECT * FROM product p,cartitem c WHERE p.pid = c.pid AND c.uid = ? ORDER BY c.orderby";
        List<CartItem> cartItemList = new ArrayList<CartItem>();
        try {
            //查出来的是多个记录，
            List<Map<String,Object>> list = runner.query(sql,new MapListHandler(),uid);
            for (Map<String,Object> m :list){
                Product product = new Product();
                CartItem cartItem = new CartItem();
                BeanUtils.populate(product,m);
                BeanUtils.populate(cartItem,m);
                cartItem.setProduct(product);//产品设置为条目的产品
                cartItemList.add(cartItem); //每次完成后，一定把对象放到集合中
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //判断集合中是否放有元素，如果长度不为0，才说明有元素，
        if (cartItemList.size()>0){
            return cartItemList;
        }
        return null;
    }

    public CartItem findByPidAndUid(int pid, int uid) {
        String sql = "select * from  cartitem WHERE pid = ? AND uid = ?";

        try {
            Map<String,Object> map = runner.query(sql,new MapHandler(),pid,uid);
            //如果没有一条记录，直接返回null不需要再封
            if (map == null){
                return null;
            }
            CartItem cartItem = new CartItem();
            Product product = new Product();
            User user = new User();
            BeanUtils.populate(cartItem,map);
            BeanUtils.populate(product,map);
            BeanUtils.populate(user,map);
            cartItem.setProduct(product);
            cartItem.setUser(user);
            return cartItem;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public int add(CartItem cartItem) {
        String sql = "insert into cartitem (cartid,quantity,pid,uid) value (?,?,?,?)";
        try {
            int count = runner.update(sql,cartItem.getCartid(),cartItem.getQuantity(),cartItem.getProduct().getPid(),cartItem.getUser().getUid());
                return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int update(CartItem cartitem) {
        String sql = "update cartitem set quantity = ? where cartid = ?";
        try {
            int count = runner.update(sql,cartitem.getQuantity(),cartitem.getCartid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    public void deleteByUid(int uid) {

    }
}
